//https://blog.csdn.net/LLL_liuhui/article/details/80361928
//测试数据
//var label_water_bala_companyId = 1; //组号
var label_eva_cmp_water_anl_unique_time = 2019-01-01; //时间
//var label_water_bala_companyId=1
var _columns =[
			{checkbox: true},
			{title: '序号', field: 'id', align: 'center',	 valign: 'middle'},
			{title: '用水区域', field: 'location', align: 'center',	 valign: 'middle'},
			{title: '用水分类', field: 'waterClass', align: 'center',	 valign: 'middle'},
			{title: '用水用途', field: 'waterUsing', align: 'center',	 valign: 'middle'},
			{title: '用水量', field: 'useWater', align: 'center',	 valign: 'middle'},
			{title: '用水量/占总用水量的比例', field: 'useProportion', align: 'center',	 valign: 'middle'},
			{title: '新水量', field: 'newWater', align: 'center',	 valign: 'middle'},
			{title: '新水量/占总新水量的比例', field: 'newProportion', align: 'center',	 valign: 'middle'},
			{title: '重复利用水量', field: 'repeatWater', align: 'center',	 valign: 'middle'},
			{title: '排水量', field: 'drainWater', align: 'center',	 valign: 'middle'},
			{title: '耗水量', field: 'consumpWater', align: 'center',	 valign: 'middle'},
			{title: '漏失水量', field: 'lossWater', align: 'center',	 valign: 'middle'},
]

var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $('#data_table').bootstrapTable({
            url: '/entity_eva_cmp_water_anl/select',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: false,                   //是否显示分页（*）
            queryParams: oTableInit.queryParams,//传递参数（*）
            sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: false,                  //是否显示所有的列
            showRefresh: true,                  //是否显示刷新按钮
            clickToSelect: true,                //是否启用点击选中行
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            columns: _columns,
            onLoadSuccess: function (data) {
            	//合并行
                mergeCells(data.rows, "location", 1, '#data_table');//无依赖行合并
                mergeCellsDepend(data.rows, "waterClass", "location", 1, '#data_table');//有依赖行合并
                selectCalSumResult()//展示計算結果
            },
            //导出
            showExport: true,                     //是否显示导出按钮
            exportTypes:['csv', 'doc', 'excel'], //['csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],           //导出文件类型
            exportDataType: "basic",             //basic当前页', 'all所有, 'selected'.
            exportOptions: {
                fileName: '企业用水分析表'+label_eva_cmp_water_anl_unique_time,  //文件名称设置
                excelstyles: ['background-color', 'color', 'font-size', 'font-weight']
            },
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码
            id: label_water_bala_companyId,//标签号，每一张表唯一标号
        };
        return temp;
    };
    return oTableInit;
};

var ButtonInit = function () {
    var oInit = new Object();

    oInit.Init = function () {
        //初始化页面上面的按钮事件
        //新增按钮
        $("#btn_add").click(function () { 
        	//默认出现生产用水模板
        	$('#newWaterClass').val("")
        	$('#newWaterUsing').val("")
        	$('#newUseWater').val("")
        	$('#newUseProportion').val("")
        	$('#newNewWater').val("")
        	$('#newNewProportion').val("")
        	$('#newRepeatWater').val("")
        	$('#newDrainWater').val("")
        	$('#newConsumpWater').val("")
        	$('#newLossWater').val("")
        	
        	//类型下拉框重组
        	$("#newLocation").empty();
        	$("#newLocation").append('<option value="生产用水">生产用水</option>');
        	$("#newLocation").append('<option value="非生产用水">非生产用水</option>');
        	
        	$("#newWaterClass").empty();
        	$("#newWaterClass").append('<option value="主要生产用水">主要生产用水</option>');
        	$("#newWaterClass").append('<option value="辅助生产用水">辅助生产用水</option>');
        	$("#newWaterClass").append('<option value="附属生产用水">附属生产用水</option>');
        	//显示重复利用水、排水量、耗水量、漏失水量
        	$("#newRepeatWaterDiv").attr("style","display:block;");//显示
        	$("#newDrainWaterDiv").attr("style","display:block;");
        	$("#newConsumpWaterDiv").attr("style","display:block;");
        	$("#newLossWaterDiv").attr("style","display:block;");
            $('#new_data').modal()
        });
        
        //編輯總計部分
        $("#edit_data_suminfo").click(function(){
        	if($("#editCalSum").attr("style") == "display:none;"){
        		$("#editCalSum").removeAttr("style");
        		$("#editCalSum").attr("style","display:block;");//show model
        	}else{
        		$("#editCalSum").removeAttr("style");
        		$("#editCalSum").attr("style","display:none;");
        	}
        });
        
        //编辑按钮
        $("#btn_edit").click(function () {
            var arrselections = $("#data_table").bootstrapTable('getSelections');
            if (arrselections.length > 1) {
                bootoast({
                    message: '只能选择一行进行编辑',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (arrselections.length <= 0) {
                bootoast({
                    message: '请选择有效数据',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            var location = arrselections[0]['location']
            $('#editLocation').val(location)
            if(location == "生产用水"){
            	$("#editRepeatWaterDiv").attr("style","display:block;");//输入框显示
            	$("#editDrainWaterDiv").attr("style","display:block;");
            	$("#editConsumpWaterDiv").attr("style","display:block;");
            	$("#editLossWaterDiv").attr("style","display:block;");
            	$("#edit_data_suminfo").show();//總計按鈕
            }else{
            	$("#editRepeatWaterDiv").attr("style","display:none;");//输入框隐藏
            	$("#editDrainWaterDiv").attr("style","display:none;");
            	$("#editConsumpWaterDiv").attr("style","display:none;");
            	$("#editLossWaterDiv").attr("style","display:none;");
            	$("#edit_data_suminfo").hide();//總計按鈕
            }  
            $("#editCalSum").attr("style","display:none;");//隐藏汇总框
            
            $('#editWaterClass').val(arrselections[0]['waterClass'])
            $('#editWaterUsing').val(arrselections[0]['waterUsing'])
            $('#editUseWater').val(arrselections[0]['useWater'])
            $('#editUseProportion').val(arrselections[0]['useProportion'])
            $('#editNewWater').val(arrselections[0]['newWater'])
            $('#editNewProportion').val(arrselections[0]['newProportion'])
            $('#editRepeatWater').val(arrselections[0]['repeatWater'])
            $('#editDrainWater').val(arrselections[0]['drainWater'])
            $('#editConsumpWater').val(arrselections[0]['consumpWater'])
            $('#editLossWater').val(arrselections[0]['lossWater'])
            selectCalSumParam()//將計算參數賦值
            $('#edit_data').modal()
            
        });

        // 删除按钮
        $("#btn_delete").click(function () {
            var arrselections = $("#data_table").bootstrapTable('getSelections');
            if (arrselections.length <= 0) {
                bootoast({
                    message: '请选择有效数据',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }

            values=[];
            for(var i=0;i<arrselections.length;i++)
                values.push(arrselections[i].id);
            
            bootbox.confirm({
                message: "确认要删除选择的数据吗？",
                locale: 'zh_CN',
                title: '操作提示',
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            type: "post",
                            url: "/entity_eva_cmp_water_anl/delete",
                            data: JSON.stringify(values),
                            contentType: "application/json",
                            success: function (data) {
                                if (data['status'] == "success") {
                                    bootoast({
                                        message: '删除成功',
                                        position: 'top',
                                        type: 'success',
                                        timeout: 1.5,
                                    });
                                    $("#data_table").bootstrapTable('refresh');
                                } else {
                                    bootoast({
                                        message: '删除失败\n' + data['msg'],
                                        position: 'top',
                                        type: 'danger',
                                        timeout: 1.5,
                                    });
                                }
                            }
                        });
                    }
                }
            })

        });
        
        //新增框内的提交
        $('#new_data_submit').on('click', function () {
            var waterClass = $('#newWaterClass').val()
            var waterUsing = $('#newWaterUsing').val()
            var useWater = $('#newUseWater').val()
            var useProportion = $('#newUseProportion').val()
            var newWater = $('#newNewWater').val()
            var newProportion = $('#newNewProportion').val()
            var repeatWater = $('#newRepeatWater').val()
            var drainWater =$('#newDrainWater').val()
            var consumpWater = $('#newConsumpWater').val()
            var lossWater = $('#newLossWater').val()
            var labelId = label_water_bala_companyId 
            var location = $('#newLocation').val();
            if (location == "生产用水"){
            	if(fieldIsEmpty(waterUsing,'用水用途不能为空','') ||
                		fieldIsEmpty(useWater,'用水量不能为空','') || !fieldIsNumber(useWater,'','用水量应为整数或者小数') || !fieldIsDecimalTrue(useWater,'NUMBER(9,2)','','用水量格式不对，请输入小数') ||
                		fieldIsEmpty(useProportion,'用水量占比不能为空','') || !fieldIsNumber(useProportion,'','用水量占比应为整数或者小数') || !fieldIsDecimalTrue(useProportion,'NUMBER(9,2)','','用水量占比格式不对，请输入小数') || fieldIsBetween0_1(useProportion,'','用水量占比应为0到1之间的数') ||
                		fieldIsEmpty(newWater,'新水量不能为空','') || !fieldIsNumber(newWater,'','新水量应为整数或者小数') || !fieldIsDecimalTrue(newWater,'NUMBER(9,2)','','新水量格式不对，请输入小数') ||
                		fieldIsEmpty(newProportion,'新水量占比不能为空','') || !fieldIsNumber(newProportion,'','新水量占比应为整数或者小数') || !fieldIsDecimalTrue(newProportion,'NUMBER(9,2)','','新水量占比格式不对，请输入小数') || fieldIsBetween0_1(newProportion,'','新水量占比应为0到1之间的数') ||
                		fieldIsEmpty(repeatWater,'重复利用水量不能为空','') || !fieldIsNumber(repeatWater,'','重复利用水量应为整数或者小数') || !fieldIsDecimalTrue(repeatWater,'NUMBER(9,2)','','重复利用水量格式不对，请输入小数') || 
                		fieldIsEmpty(drainWater,'排水量不能为空','') || !fieldIsNumber(drainWater,'','排水量应为整数或者小数') || !fieldIsDecimalTrue(drainWater,'NUMBER(9,2)','','排水量格式不对，请输入小数') ||
                		fieldIsEmpty(consumpWater,'耗水量不能为空','') || !fieldIsNumber(consumpWater,'','耗水量应为整数或者小数') || !fieldIsDecimalTrue(consumpWater,'NUMBER(9,2)','','耗水量格式不对，请输入小数') ||
                		fieldIsEmpty(lossWater,'漏失水量不能为空','') || !fieldIsNumber(lossWater,'','漏失水量应为整数或者小数') || !fieldIsDecimalTrue(lossWater,'NUMBER(9,2)','','漏失水量格式不对，请输入小数'))
            	{
                	return false;
            	}
            }
            else{
            	if(fieldIsEmpty(waterUsing,'用水用途不能为空','') ||
                		fieldIsEmpty(useWater,'用水量不能为空','') || !fieldIsNumber(useWater,'','用水量应为整数或者小数') || !fieldIsDecimalTrue(useWater,'NUMBER(9,2)','','用水量格式不对，请输入小数') ||
                		fieldIsEmpty(useProportion,'用水量占比不能为空','') || !fieldIsNumber(useProportion,'','用水量占比应为整数或者小数') || !fieldIsDecimalTrue(useProportion,'NUMBER(9,2)','','用水量占比格式不对，请输入小数') || fieldIsBetween0_1(useProportion,'','用水量占比应为0到1之间的数') ||
                		fieldIsEmpty(newWater,'新水量不能为空','') || !fieldIsNumber(newWater,'','新水量应为整数或者小数') || !fieldIsDecimalTrue(newWater,'NUMBER(9,2)','','新水量格式不对，请输入小数') ||
                		fieldIsEmpty(newProportion,'新水量占比不能为空','') || !fieldIsNumber(newProportion,'','新水量占比应为整数或者小数') || !fieldIsDecimalTrue(newProportion,'NUMBER(9,2)','','新水量占比格式不对，请输入小数') ||fieldIsBetween0_1(newProportion,'','新水量占比应为0到1之间的数')) 
            		return false;
            }
            var dataDic = {
            	'waterClass': waterClass,
            	'waterUsing': waterUsing,
                'useWater':  useWater,
                'useProportion': useProportion,
                'newWater': newWater,
                'newProportion': newProportion,
                'labelId': label_water_bala_companyId,
                'location': location,	
            }
            if (location == "生产用水"){
            	dataDic['repeatWater'] = repeatWater
            	dataDic['drainWater'] = drainWater
            	dataDic['consumpWater'] = consumpWater
            	dataDic['lossWater'] = lossWater
            }
            $.ajax({
                type: "get",
                url: "/entity_eva_cmp_water_anl/insert",
                data: dataDic,
                success: function (data) {
                    if (data['status'] == "success") {
                        bootoast({
                            message: '插入成功',
                            position: 'top',
                            type: 'success',
                            timeout: 2,
                        });
                        $("#new_data").modal('hide');
                        $('#newWaterClass').val("")
                    	$('#newWaterUsing').val("")
                    	$('#newUseWater').val("")
                    	$('#newUseProportion').val("")
                    	$('#newNewWater').val("")
                    	$('#newNewProportion').val("")
                    	$('#newRepeatWater').val("")
                    	$('#newDrainWater').val("")
                    	$('#newConsumpWater').val("")
                    	$('#newLossWater').val("")
                        $("#data_table").bootstrapTable('refresh');
                    } else {
                        bootoast({
                            message: '插入失败\n' + data['msg'],
                            position: 'top',
                            type: 'danger',
                            timeout: 2,
                        });
                    }
                },
                error: function () {
                    bootoast({
                        message: '通讯异常',
                        position: 'top',
                        type: 'danger',
                        timeout: 1.5,
                    });
                }
            });
        });
        
        //编辑框内的提交
        $('#edit_data_submit').click(function () {
        	var values = []
        	var id = $("#data_table").bootstrapTable('getSelections')[0]['id'];        
        	var waterClass = $('#editWaterClass').val()
            var waterUsing = $('#editWaterUsing').val()
            var useWater = $('#editUseWater').val()
            var useProportion = $('#editUseProportion').val()
            var newWater = $('#editNewWater').val()
            var newProportion = $('#editNewProportion').val()
            var repeatWater = $('#editRepeatWater').val()
            var drainWater =$('#editDrainWater').val()
            var consumpWater = $('#editConsumpWater').val()
            var lossWater = $('#editLossWater').val()
            var labelId = label_water_bala_companyId 
            var location = $('#editLocation').val() 
             if (location == "生产用水"){
            	 if(fieldIsEmpty(waterUsing,'用水用途不能为空','') ||
                 		fieldIsEmpty(useWater,'用水量不能为空','') || !fieldIsNumber(useWater,'','用水量应为整数或者小数') || !fieldIsDecimalTrue(useWater,'NUMBER(9,2)','','用水量格式不对，请输入小数') ||
                 		fieldIsEmpty(useProportion,'用水量占比不能为空','') || !fieldIsNumber(useProportion,'','用水量占比应为整数或者小数') || !fieldIsDecimalTrue(useProportion,'NUMBER(9,2)','','用水量占比格式不对，请输入小数') || fieldIsBetween0_1(useProportion,'','用水量占比应为0到1之间的数') ||
                 		fieldIsEmpty(newWater,'新水量不能为空','') || !fieldIsNumber(newWater,'','新水量应为整数或者小数') || !fieldIsDecimalTrue(newWater,'NUMBER(9,2)','','新水量格式不对，请输入小数') ||
                 		fieldIsEmpty(newProportion,'新水量占比不能为空','') || !fieldIsNumber(newProportion,'','新水量占比应为整数或者小数') || !fieldIsDecimalTrue(newProportion,'NUMBER(9,2)','','新水量占比格式不对，请输入小数') || fieldIsBetween0_1(newProportion,'','新水量占比应为0到1之间的数') ||
                 		fieldIsEmpty(repeatWater,'重复利用水量不能为空','') || !fieldIsNumber(repeatWater,'','重复利用水量应为整数或者小数') || !fieldIsDecimalTrue(repeatWater,'NUMBER(9,2)','','重复利用水量格式不对，请输入小数') ||
                 		fieldIsEmpty(drainWater,'排水量不能为空','') || !fieldIsNumber(drainWater,'','排水量应为整数或者小数') || !fieldIsDecimalTrue(drainWater,'NUMBER(9,2)','','排水量格式不对，请输入小数') ||
                 		fieldIsEmpty(consumpWater,'耗水量不能为空','') || !fieldIsNumber(consumpWater,'','耗水量应为整数或者小数') || !fieldIsDecimalTrue(consumpWater,'NUMBER(9,2)','','耗水量格式不对，请输入小数') ||
                 		fieldIsEmpty(lossWater,'漏失水量不能为空','') || !fieldIsNumber(lossWater,'','漏失水量应为整数或者小数') || !fieldIsDecimalTrue(lossWater,'NUMBER(9,2)','','漏失水量格式不对，请输入小数'))
             	{
                 	return false;
             	} 
             }
             else{
            	 if(fieldIsEmpty(waterUsing,'用水用途不能为空','') ||
             		fieldIsEmpty(useWater,'用水量不能为空','') || !fieldIsNumber(useWater,'','用水量应为整数或者小数') || !fieldIsDecimalTrue(useWater,'NUMBER(9,2)','','用水量格式不对，请输入小数') ||
            		fieldIsEmpty(useProportion,'用水量占比不能为空','') || !fieldIsNumber(useProportion,'','用水量占比应为整数或者小数') || !fieldIsDecimalTrue(useProportion,'NUMBER(9,2)','','用水量占比格式不对，请输入小数') || fieldIsBetween0_1(useProportion,'','用水量占比应为0到1之间的数') ||
            		fieldIsEmpty(newWater,'新水量不能为空','') || !fieldIsNumber(newWater,'','新水量应为整数或者小数') || !fieldIsDecimalTrue(newWater,'NUMBER(9,2)','','新水量格式不对，请输入小数') ||
            		fieldIsEmpty(newProportion,'新水量占比不能为空','') || !fieldIsNumber(newProportion,'','新水量占比应为整数或者小数') || !fieldIsDecimalTrue(newProportion,'NUMBER(9,2)','','新水量占比格式不对，请输入小数') || fieldIsBetween0_1(newProportion,'','新水量占比应为0到1之间的数'))
            		return false;
             }
            	 
        	var calParam11 = $('#editCalParam11').val();
        	var calParam12 = $('#editCalParam12').val();
        	var calParam21 = $('#editCalParam21').val();
        	var calParam22 = $('#editCalParam22').val();
        	var calParam31 = $('#editCalParam31').val();
        	var calParam32 = $('#editCalParam32').val();
        	var calParam41 = $('#editCalParam41').val();
        	var calParam42 = $('#editCalParam42').val();
        	var calParam51 = $('#editCalParam51').val();
        	var calParam52 = $('#editCalParam52').val();
        	var calParam61 = $('#editCalParam61').val();
        	var calParam62 = $('#editCalParam62').val();
        	var calParam71 = $('#editCalParam71').val();
        	var calParam72 = $('#editCalParam72').val();
        	var calParam81 = $('#editCalParam81').val();
        	var calParam82 = $('#editCalParam82').val();
        	var calParam91 = $('#editCalParam91').val();
        	var calParam92 = $('#editCalParam92').val();
            var calParam01 = $('#editCalParam01').val();
        	var calParam02 = $('#editCalParam02').val();
        	if (location == "生产用水"){
	        	if(fieldIsEmpty(calParam12,'公式1中产品产量不能为空','') || !fieldIsNumber(calParam12,'','公式1中产品产量应为整数或者小数') || !fieldIsDecimalTrue(calParam12,'NUMBER(9,2)','','公式1中产品产量格式不对，请输入小数') || fieldIsLessZero(calParam12,'','公式1中产品产量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam21,'公式2中直接冷却水循环量不能为空','') || !fieldIsNumber(calParam21,'','公式2中直接冷却水循环量应为整数或者小数') || !fieldIsDecimalTrue(calParam21,'NUMBER(9,2)','','公式2中直接冷却水循环量格式不对，请输入小数') || fieldIsLessZero(calParam21,'','公式2中直接冷却水循环量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam22,'公式2中直接冷却水循环系统补充水量不能为空','') || !fieldIsNumber(calParam22,'','公式2中直接冷却水循环系统补充水量应为整数或者小数') || !fieldIsDecimalTrue(calParam22,'NUMBER(9,2)','','公式2中直接冷却水循环系统补充水量格式不对，请输入小数') || fieldIsLessZero(calParam22,'','公式2中直接冷却水循环系统补充水量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam31,'公式3中蒸汽冷凝水回用量不能为空','') || !fieldIsNumber(calParam31,'','公式3中蒸汽冷凝水回用量应为整数或者小数') || !fieldIsDecimalTrue(calParam31,'NUMBER(9,2)','','公式3中蒸汽冷凝水回用量格式不对，请输入小数') || fieldIsLessZero(calParam31,'','公式3中蒸汽冷凝水回用量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam32,'公式3中产汽设备产汽量不能为空','') || !fieldIsNumber(calParam32,'','公式3中产汽设备产汽量应为整数或者小数') || !fieldIsDecimalTrue(calParam32,'NUMBER(9,2)','','公式3中产汽设备产汽量格式不对，请输入小数') || fieldIsLessZero(calParam32,'','公式3中产汽设备产汽量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam51,'公式5中企业达到排放标准排水量不能为空','') || !fieldIsNumber(calParam51,'','公式5中企业达到排放标准排水量应为整数或者小数') || !fieldIsDecimalTrue(calParam51,'NUMBER(9,2)','','公式5中企业达到排放标准排水量格式不对，请输入小数') || fieldIsLessZero(calParam51,'','公式5中企业达到排放标准排水量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam52,'公式5中企业排水量不能为空','') || !fieldIsNumber(calParam52,'','公式5中企业排水量应为整数或者小数') || !fieldIsDecimalTrue(calParam52,'NUMBER(9,2)','','公式5中企业排水量格式不对，请输入小数') || fieldIsLessZero(calParam52,'','公式5中企业排水量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam71,'公式7中间接冷却水循环量不能为空','') || !fieldIsNumber(calParam71,'','公式7中间接冷却水循环量应为整数或者小数') || !fieldIsDecimalTrue(calParam71,'NUMBER(9,2)','','公式7中间接冷却水循环量格式不对，请输入小数') || fieldIsLessZero(calParam71,'','公式7中间接冷却水循环量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam72,'公式7中间接冷却水循环系统补充水量不能为空','') || !fieldIsNumber(calParam72,'','公式7中间接冷却水循环系统补充水量应为整数或者小数') || !fieldIsDecimalTrue(calParam72,'NUMBER(9,2)','','公式7中间接冷却水循环系统补充水量格式不对，请输入小数') || fieldIsLessZero(calParam72,'','公式7中间接冷却水循环系统补充水量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam81,'公式8中企业外排水量不能为空','') || !fieldIsNumber(calParam81,'','公式8中企业外排水量应为整数或者小数') || !fieldIsDecimalTrue(calParam81,'NUMBER(9,2)','','公式8中企业外排水量格式不对，请输入小数') || fieldIsLessZero(calParam81,'','公式8中企业外排水量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam91,'公式9中废水回用量不能为空','') || !fieldIsNumber(calParam91,'','公式9中废水回用量应为整数或者小数') || !fieldIsDecimalTrue(calParam91,'NUMBER(9,2)','','公式9中废水回用量格式不对，请输入小数') || fieldIsLessZero(calParam91,'','公式9中废水回用量必须大于等于0，请重新输入') ||
	        			fieldIsEmpty(calParam92,'公式9中废水外排水量不能为空','') || !fieldIsNumber(calParam92,'','公式9中废水外排水量应为整数或者小数') || !fieldIsDecimalTrue(calParam92,'NUMBER(9,2)','','公式9中废水外排水量格式不对，请输入小数') || fieldIsLessZero(calParam92,'','公式9中废水外排水量必须大于等于0，请重新输入'))
	        	{
	            	return false;
	        	}
        	}
            var dataDic = {
        		'id': id,
            	'waterClass': waterClass,
            	'waterUsing': waterUsing,
                'useWater':  useWater,
                'useProportion': useProportion,
                'newWater': newWater,
                'newProportion': newProportion,
                'labelId': labelId,
                'location': location,	
            }
            if (location == "生产用水"){
            	dataDic['repeatWater'] = repeatWater
            	dataDic['drainWater'] = drainWater
            	dataDic['consumpWater'] = consumpWater
            	dataDic['lossWater'] = lossWater
            }
        	//详细参数更细
        	$.ajax({
        		type: "post",
        		url: "/entity_eva_cmp_water_anl/update",
        		data: dataDic,
        		success: function (data) {
        			if (data['status'] == "success") {
        				bootoast({
        					message: '修改成功',
        					position: 'top',
        					type: 'success',
        					timeout: 2,
        				});
        				$("#edit_data").modal('hide');
        				$("#data_table").bootstrapTable('refresh');
        			} else {
        				bootoast({
        					message: '修改失败\n' + data['msg'],
        					position: 'top',
        					type: 'danger',
        					timeout: 2,
        				});	
        			}
        		},
        		error: function () {
        			bootoast({
        				message: '通讯异常',
        				position: 'top',
        				type: 'danger',
        				timeout: 1.5,
        			});
        		}
        	});
        	//汇总参数更新
        	//console.log("label_water_bala_companyId: " + label_water_bala_companyId);



        	$.ajax({
        		type: "post",
        		url: "/label_eva_cmp_water_anl/updateVariable",
        		data: {
        			'id': label_water_bala_companyId,
                    'formula1Variable1': calParam11,
                    'formula1Variable2': calParam12,
                    'formula2Variable1': calParam21,
                    'formula2Variable2': calParam22,
                    'formula3Variable1': calParam31,
                    'formula3Variable2': calParam32,
                    'formula4Variable1': calParam41,
                    'formula4Variable2': calParam42,
                    'formula5Variable1': calParam51,
                    'formula5Variable2': calParam52,
                    'formula6Variable1': calParam61,
                    'formula6Variable2': calParam62,
                    'formula7Variable1': calParam71,
                    'formula7Variable2': calParam72,
                    'formula8Variable1': calParam81,
                    'formula8Variable2': calParam82,
                    'formula9Variable1': calParam91,
                    'formula9Variable2': calParam92,
                    'formula10Variable1': calParam01,
                    'formula10Variable2': calParam02
        		},
        		success: function (data) {
        			if (data['status'] == "success") {
        				bootoast({
        					message: '修改計算數據成功',
        					position: 'top',
        					type: 'success',
        					timeout: 2,
        				});
        				$("#edit_data").modal('hide');
        				$("#data_table").bootstrapTable('refresh');
        			} else {
        				bootoast({
        					message: '修改計算數據失败\n' + data['msg'],
        					position: 'top',
        					type: 'danger',
        					timeout: 2,
        				});	
        			}
        		},
        		error: function () {
        			bootoast({
        				message: '通讯异常',
        				position: 'top',
        				type: 'danger',
        				timeout: 1.5,
        			});
        		}
        	});
        });
    }
    return oInit;
};

$(function () {
    //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();

    //2.初始化Button的点击事件
    var oButtonInit = new ButtonInit();
    oButtonInit.Init();
    
    //显示时间
    $("#nowdate").text(label_eva_cmp_water_anl_unique_time);
    
    //添加总计行
    add_table_foot()
        
    //3.隐藏列
    $('#data_table').bootstrapTable('hideColumn', 'id');
});

//返回上级页面
$("#btn_return").click(function () {
   label_water_bala_companyId=label_entity_water_bala_companyID;
   $('#page-wrapper').load('pages/label_entity_water_bala.html');
});

/**
 * 有依赖合并行
 * @param data  原始数据（在服务端完成排序）
 * @param fieldName 合并属性名称数组
 * @param fieldNameBefore 合并属性名称数组的依赖列名
 * @param colspan 列数
 * @param target 目标表格对象
 */
function mergeCellsDepend(data, fieldName, filedNameBefore, colspan, target) {
	
    if (data.length == 0) {
//         alert("不能传入空数据");
         return;
    }
    var numArr = [];
    var value = data[0][fieldName];
    var value_b = data[0][filedNameBefore];
    var num = 0;
    for (var i = 0; i < data.length; i++) {
        if (value_b != data[i][filedNameBefore] || value != data[i][fieldName]) {
            numArr.push(num);
            value_b = data[i][filedNameBefore];
            value = data[i][fieldName];
            num = 1;
            continue;
        }
        num++;
    }
    numArr.push(num);
    
    var merIndex = 0;
    for (var i = 0; i < numArr.length; i++) {
        $(target).bootstrapTable('mergeCells', { index: merIndex, field: fieldName, colspan: colspan, rowspan: numArr[i] })
        merIndex += numArr[i];
    }
}

/**
 * 无依赖合并行
 * @param data  原始数据（在服务端完成排序）
 * @param fieldName 合并属性名称数组
 * @param colspan 列数
 * @param target 目标表格对象
 */
function mergeCells(data, fieldName, colspan, target) {
	
    if (data.length == 0) {
//         alert("不能传入空数据");
         return;
    }
    var numArr = [];
    var value = data[0][fieldName];
    var num = 0;
    for (var i = 0; i < data.length; i++) {
        if (value != data[i][fieldName]) {
            numArr.push(num);
            value = data[i][fieldName];
            num = 1;
            continue;
        }
        num++;
    }
    numArr.push(num);
    
    var merIndex = 0;
    for (var i = 0; i < numArr.length; i++) {
        $(target).bootstrapTable('mergeCells', { index: merIndex, field: fieldName, colspan: colspan, rowspan: numArr[i] })
        merIndex += numArr[i];
    }
}

function newModelEventChange(){
    if($('#newLocation').val() == "生产用水"){
    	$("#newRepeatWaterDiv").attr("style","display:block;");//显示
    	$("#newDrainWaterDiv").attr("style","display:block;");
    	$("#newConsumpWaterDiv").attr("style","display:block;");
    	$("#newLossWaterDiv").attr("style","display:block;");
    	
    	$("#newWaterClass").empty();//下拉框重组
    	$("#newWaterClass").append('<option value="主要生产用水">主要生产用水</option>');
    	$("#newWaterClass").append('<option value="辅助生产用水">辅助生产用水</option>');
    	$("#newWaterClass").append('<option value="附属生产用水">附属生产用水</option>');
    }else{//非生产用水
    	$("#newRepeatWaterDiv").attr("style","display:none;");//隐藏
    	$("#newDrainWaterDiv").attr("style","display:none;");
    	$("#newConsumpWaterDiv").attr("style","display:none;");
    	$("#newLossWaterDiv").attr("style","display:none;");
    	
    	$("#newWaterClass").empty();//下拉框重组
    	$("#newWaterClass").append('<option value="非生产用水">非生产用水</option>');
    }
}

function editModelEventChange(){
    if($('#editLocation').val() == "生产用水"){
    	$("#editRepeatWaterDiv").attr("style","display:block;");//显示
    	$("#editDrainWaterDiv").attr("style","display:block;");
    	$("#editConsumpWaterDiv").attr("style","display:block;");
    	$("#editLossWaterDiv").attr("style","display:block;");
    }else{
    	$("#editRepeatWaterDiv").attr("style","display:none;");//隐藏
    	$("#editDrainWaterDiv").attr("style","display:none;");
    	$("#editConsumpWaterDiv").attr("style","display:none;");
    	$("#editLossWaterDiv").attr("style","display:none;");
    }
}

/**
 * 显示汇总参数数据
 * @returns
 */
function selectCalSumParam(){
	//ajax交互后台的数据，同步
	var datatmp;
	$.ajax({
		url: "/label_eva_cmp_water_anl/selectMiddleVariable",
		type: "get",
		dataType : "json",//数据格式 
    	async: false,
    	data: {"id": label_water_bala_companyId},
    	contentType: "application/json; charset=utf-8",
		success: function (data) {
			datatmp = data;
		}
	});


	$('#editCalParam11').val(datatmp.rows.formula1Variable1);
	$('#editCalParam12').val(datatmp.rows.formula1Variable2);
	$('#editCalParam21').val(datatmp.rows.formula2Variable1);
	$('#editCalParam22').val(datatmp.rows.formula2Variable2);
	$('#editCalParam31').val(datatmp.rows.formula3Variable1);
	$('#editCalParam32').val(datatmp.rows.formula3Variable2);
	$('#editCalParam41').val(datatmp.rows.formula4Variable1);
	$('#editCalParam42').val(datatmp.rows.formula4Variable2);
	$('#editCalParam51').val(datatmp.rows.formula5Variable1);
	$('#editCalParam52').val(datatmp.rows.formula5Variable2);
	$('#editCalParam61').val(datatmp.rows.formula6Variable1);
	$('#editCalParam62').val(datatmp.rows.formula6Variable2);
	$('#editCalParam71').val(datatmp.rows.formula7Variable1);
	$('#editCalParam72').val(datatmp.rows.formula7Variable2);
	$('#editCalParam81').val(datatmp.rows.formula8Variable1);
	$('#editCalParam82').val(datatmp.rows.formula8Variable2);
	$('#editCalParam91').val(datatmp.rows.formula9Variable1);
	$('#editCalParam92').val(datatmp.rows.formula9Variable2);
	$('#editCalParam01').val(datatmp.rows.formula10Variable1);
	$('#editCalParam02').val(datatmp.rows.formula10Variable2);
	
}

/**
 * 显示汇总结果
 * @returns
 */
function selectCalSumResult(){
	//ajax交互后台的数据，同步
	var datatmp;
	$.ajax({
		type: "get",
		url: "/label_eva_cmp_water_anl/selectSumResult",
		dataType : "json",//数据格式 
    	async: false,
    	contentType: "application/json; charset=utf-8",
    	data: {'id': label_water_bala_companyId},
		success: function (data) {
			datatmp = data;
		}
	});
	if(datatmp.status=='success'){
	    $('#sum_use_water').text(datatmp.rows.useWaterSuml)//datatmp.sum_use_water);
        	$('#sum_new_water').text(datatmp.rows.newWaterSuml)//datatmp.sum_new_water);
        	$('#sum_repeat_water').text(datatmp.rows.repeatWaterSuml)//datatmp.sum_repeat_water);
        	$('#sum_drain_water').text(datatmp.rows.drainWaterSuml)//datatmp.sum_drain_water);
        	$('#sum_consump_water').text(datatmp.rows.consumpWaterSuml)//datatmp.sum_consump_water);
        	$('#sum_loss_water').text(datatmp.rows.lossWaterSuml)//datatmp.sum_loss_water);
        	$('#nsum_use_water').text(datatmp.rows.useWaterSumn)//datatmp.nsum_use_water);
        	$('#nsum_new_water').text(datatmp.rows.newWaterSumn)//datatmp.nsum_new_water);
        	$('#sum01').text(datatmp.rows.unitProductWaterWithdrawal)
        	$('#sum02').text(datatmp.rows.dirCooWaterCircuRate)
        	$('#sum03').text(datatmp.rows.steamCondensateReuseRate)
        	$('#sum04').text(datatmp.rows.lossRate)
        	$('#sum05').text(datatmp.rows.wasteWaterDischargeRate)
        	$('#sum06').text(datatmp.rows.repeatUtilizationRate)
        	$('#sum07').text(datatmp.rows.indirCooWaterCircuRate)
        	$('#sum08').text(datatmp.rows.drainageRate)
        	$('#sum09').text(datatmp.rows.wasteWaterReuseRate)
        	$('#sum010').text(datatmp.rows.unconWaterReplaceRate)
	}


}

//添加总计行
function add_table_foot() {
	var title1 = "单位产品取水量计算";
	var param11 = "取水量";
	var param12 = "产品产量";
	var bark1  = "备注：单位产品取水量=取水量÷产品产量 ,取水量为企业水平衡测试统计表的常规水资源量汇总量";
	
	var title2 = "直接冷却水循环率计算";
	var param21 = "直接冷却水循环量";
	var param22 = "直接冷却水循环系统补充水量";
	var bark2  = "备注：直接冷却水循环率=直接冷却水循环量÷(直接冷却水循环系统补充水量+直接冷却水循环量)×100% 直接冷却水循环系统补充水量，直接冷却水循环量单位为立方米每小时";

	var title3 = "蒸汽冷凝水回用率计算";
	var param31 = "蒸汽冷凝水回用量";
	var param32 = "产汽设备产汽量";
	var bark3  = "备注：蒸汽冷凝水回用率=蒸汽冷凝水回用量÷产汽设备产汽量×蒸汽体积质量×100% 蒸汽体积质量是常量，取值1吨每立方米 蒸汽冷凝水回用量单位为立方米每小时,产汽设备产汽量单位为吨每小时";
	
	var title4 = "漏失率计算";
	var param41 = "漏失水量";
	var param42 = "新水量";
	var bark4  = "备注：漏失率=漏失水量÷新水量×100% 漏失水量,新水量都是表中的汇总数据";
	
	var title5 = "废水达标排放率计算";
	var param51 = "企业达到排放标准排水量";
	var param52 = "企业排水量";
	var bark5  = "备注：废水达标排放率=企业达到排放标准排水量÷企业排水量×100% 企业达到排放标准排水量，企业排水量单位为立方米";
	
	var title6 = "重复利用率计算";
	var param61 = "重复利用水量";
	var param62 = "新水量";
	var bark6  = "备注：重复利用率=重复利用水量÷(重复利用水量+新水量) 重复利用水量,新水量为表中已有汇总数据";
	
	var title7 = "间接冷却水循环率计算";
	var param71 = "间接冷却水循环量";
	var param72 = "间接冷却水循环系统补充水量";
	var bark7  = "备注：间接冷却水循环率=间接冷却水循环量÷(间接冷却水循环量+间接冷却水循环系统补充水量)×100% 间接冷却水循环量,间接冷却水循环系统补充水量单位都为立方米每小时";
	
	var title8 = "排水率计算";
	var param81 = "企业外排水量";
	var param82 = "新水量";
	var bark8  = "备注：排水率=企业外排水量÷新水量 企业外排水量,新水量单位都为立方米,新水量为表中汇总数据";
	
	var title9 = "废水回用率计算";
	var param91 = "废水回用量";
	var param92 = "废水外排水量";
	var bark9  = "注：废水回用率=废水回用量÷(废水回用量+废水外排水量)×100% 废水回用量,废水外排水量单位都为立方米";
	
	var title10 = "非常规水资源替代率计算";
	var param101 = "非常规水资源利用量";
	var param102 = "常规水资源量";
	var bark10  = "备注：非常规水资源替代率=非常规水资源利用量÷(非常规水资源利用量+常规水资源量)×100% 非常规水资源利用量,常规水资源量单位都为立方米,为“企业水平衡测试统计表”的“常规水资源量汇总量”";
	
	$('#data_table').append('<tfoot>\
			<tr><th class="bs-checkbox" style="width: 36px; " data-field="0"><div></div><div/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="">生产用水总计</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum_use_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum_new_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum_repeat_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum_drain_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum_consump_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum_loss_water">0</div><div class="fht-cell"/></th>\
			</tr>\
			<tr> \
				<th class="bs-checkbox" style="width: 36px; " data-field="0"><div></div><div/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title1 + '\',\''+ param11 +'\',\''+ param12 +'\',\''+ bark1 +'\',1)">单位产品取水量：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum01"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title2 + '\',\''+ param21 +'\',\''+ param22 +'\',\''+ bark2 +'\',2)">直接冷却水循环率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum02"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title3 + '\',\''+ param31 +'\',\''+ param32 +'\',\''+ bark3 +'\',3)">冷却水回用率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum03"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title4 + '\',\''+ param41 +'\',\''+ param42 +'\',\''+ bark4 +'\',4)">漏失率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum04"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title5 + '\',\''+ param51 +'\',\''+ param52 +'\',\''+ bark5 +'\',5)">达标排放率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum05"></div><div class="fht-cell"/></th>\
			</tr>\
			<tr> \
				<th class="bs-checkbox" style="width: 36px; " data-field="0"><div></div><div/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title6 + '\',\''+ param61 +'\',\''+ param62 +'\',\''+ bark6 +'\',6)">重复利用率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum06"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title7 + '\',\''+ param71 +'\',\''+ param72 +'\',\''+ bark7 +'\',7)">间接冷却水循环率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum07"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title8 + '\',\''+ param81 +'\',\''+ param82 +'\',\''+ bark8 +'\',8)">排水率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum08"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title9 + '\',\''+ param91 +'\',\''+ param92 +'\',\''+ bark9 +'\',9)">废水回用率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum09"></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="" onclick="unit_water(\''+ title10 + '\',\''+ param101 +'\',\''+ param102 +'\',\''+ bark10 +'\',10)">非常规水资源替代率：</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="sum010"></div><div class="fht-cell"/></th>\
			</tr>\
			<tr> \
				<th class="bs-checkbox" style="width: 36px; " data-field="0"><div></div><div/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="">非生产用水总计</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="nsum_use_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id="nsum_new_water">0</div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
				<th style="text-align: center; vertical-align: middle; " data-field=""><div class="th-inner" id=""></div><div class="fht-cell"/></th>\
			</tr>\
			<tr>\
				<th class="bs-checkbox" style="width: 36px; " data-field="0"><div></div><div/></th>\
        		<td colspan="12" >注：各用水指标的计算方法参见GB/T 7119 </td>\
			</tr>\
			</tfoot>')
}